package com.jdjc.subject.application.controller;

import com.google.common.base.Preconditions;
import com.jdjc.subject.application.convert.SubjectAnswerDTOConverter;
import com.jdjc.subject.application.convert.SubjectInfoDTOConverter;
import com.jdjc.subject.application.dto.SubjectInfoDTO;
import com.jdjc.subject.common.entity.PageResult;
import com.jdjc.subject.common.entity.Result;
import com.jdjc.subject.infra.basic.entity.SubjectInfoEs;
import com.jdjc.suject.domin.entity.SubjectAnswerBO;
import com.jdjc.suject.domin.entity.SubjectInfoBO;
import com.jdjc.suject.domin.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题controller
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" SubjectController：add:dto:{}", subjectInfoDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。

            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryIds(), "分类ids不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelIds(), "标签ids不能为空");
            //自定义类型装换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);
            if (log.isInfoEnabled()) {
                log.info(" SubjectController：add:bo:{}", subjectInfoBO);
            }
            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertDTOListToBOList(subjectInfoDTO.getOptionList());

            subjectInfoBO.setOptionList(subjectAnswerBOList);

            //新增
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController：add:error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }

    }
    /**
     * 查询题目列表
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" SubjectController：getSubjectPage:dto:{}", subjectInfoDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。

            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            //自定义类型装换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);

            PageResult<SubjectInfoBO> boPageResult=subjectInfoDomainService.getSubjectPage(subjectInfoBO);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectController：getSubjectPage:error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目列表失败");
        }
    }



    /**
     * 查询题目信息
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info(" SubjectController：querySubjectInfo:dto:{}", subjectInfoDTO);
            }
            //断言 在代码中插入特定的语句来检查某个条件是否为真。如果条件为假，即断言失败，程序通常会抛出一个异常并终止执行。

            Preconditions.checkNotNull(subjectInfoDTO.getId(), "id不能为空");
            //自定义类型装换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);

            SubjectInfoBO resultBO=subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO resultDTO = SubjectInfoDTOConverter.INSTANCE.convertBOToInfoDTO(resultBO);
            return Result.ok(resultDTO);
        } catch (Exception e) {
            log.error("SubjectController：querySubjectInfo:error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * es全文检索
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            log.info(" SubjectController：getSubjectPageBySearch:dto:{}", subjectInfoDTO);
            Preconditions.checkNotNull(subjectInfoDTO.getKeyWord(), "关键词不能为空");
            //自定义类型装换
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToInfoBO(subjectInfoDTO);
            log.info(" SubjectController：getSubjectPageBySearch:bo:{}", subjectInfoBO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoEs> pageResult= subjectInfoDomainService.getSubjectPageBySearch(subjectInfoBO);
            return Result.ok(pageResult);
        } catch (Exception e) {
            log.error("SubjectController：getSubjectPageBySearch:error:{}", e.getMessage(), e);
            return Result.fail("es全文检索失败");
        }
    }
    /**
     * 获取题目的贡献榜单
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> subjectInfoBOList=subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO> subjectInfoDTOList = SubjectInfoDTOConverter.INSTANCE.convertInfoBOListToDTOList(subjectInfoBOList);
            return Result.ok(subjectInfoDTOList);
        } catch (Exception e) {
            log.error("SubjectController：getContributeList:error:{}", e.getMessage(), e);
            return Result.fail("获取题目的贡献榜单失败");
        }
    }
}
